package com.pxmeta.party.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxmeta.common.exception.ServiceException;
import com.pxmeta.party.entity.TurntableNumber;
import com.pxmeta.party.mapper.TurntableNumberMapper;
import com.pxmeta.party.service.ITurntableNumberService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 语聊派对表 服务实现类
 * </p>
 *
 * @author wcl
 * @since 2022-06-25
 */
@Service
@RequiredArgsConstructor
public class TurntableNumberServiceImpl extends ServiceImpl<TurntableNumberMapper, TurntableNumber> implements ITurntableNumberService {

    @Override
    public TurntableNumber getTurntableNumber(Integer type) {
        LambdaQueryWrapper<TurntableNumber> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TurntableNumber::getType, type);
        TurntableNumber turntableNumber = this.getOne(wrapper,false);
        return turntableNumber;
    }

    @Override
    public TurntableNumber getTurntableWormhole(){
        LambdaQueryWrapper<TurntableNumber> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TurntableNumber::getType, 0);

        return this.getOne(wrapper);
    }

    @Override
    public TurntableNumber getLastAstraNumber(Integer type) {
        List<TurntableNumber> turntableNumbers = baseMapper.getLastAstraNumbers(type);

        if (turntableNumbers.isEmpty()){
            TurntableNumber turntableNumber = new TurntableNumber();
            turntableNumber.setGiftNumber(BigDecimal.ZERO);
            turntableNumber.setConsumeNumber(BigDecimal.ZERO);
            turntableNumber.setType(7);
            turntableNumber.setCreateTime(LocalDateTime.now());
        }
        return turntableNumbers.get(0);
    }

    @Override
    public void updateAdAstraMoney(Integer status, Integer money) {

        if (money ==null){
            throw new ServiceException("参数不能为空");
        }
        TurntableNumber turntableNumber = getTurntableNumber(7);
        BigDecimal value= BigDecimal.ZERO;

        if (status == -1){
            value = turntableNumber.getConsumeNumber().subtract(BigDecimal.valueOf(money));
        }else {
            value = turntableNumber.getConsumeNumber().add(BigDecimal.valueOf(money));
        }
        turntableNumber.setConsumeNumber(value);

        this.updateById(turntableNumber);
    }

    @Override
    public void updateAdAstraMoneyProbability(Double probability) {
        TurntableNumber turntableNumber = getTurntableNumber(7);
        turntableNumber.setRebateRatio(probability);
        this.updateById(turntableNumber);
    }
}
